package com.gin.no_gay_alliance.manager;

import static com.gin.no_gay_alliance.app.MyApplication.getClient;

import androidx.annotation.NonNull;

import com.gin.nga.callback.JsonCallback;
import com.gin.nga.exception.NgaException;
import com.gin.nga.method.NukeApi;
import com.gin.nga.params.nuke.pm.PmReadParam;
import com.gin.nga.response.body.nuke.PmListBody;
import com.gin.nga.response.body.nuke.PmReplyBody;
import com.gin.no_gay_alliance.callback.Callback;

import java.io.IOException;

import okhttp3.Call;

/**
 * 私信管理器
 *
 * @author : ginstone
 * @version : v1.0.0
 * @since : 2023/8/31 16:03
 **/
public class PriMsgManager {

    /**
     * 查询私信列表
     *
     * @param page     页码
     * @param callback 回调
     */
    public void list(int page, Callback<PmListBody> callback) {
        NukeApi.pmList(getClient(), page).async(new JsonCallback<PmListBody>() {
            @Override
            public void onFailure(@NonNull Call call, @NonNull IOException e) {
                super.onFailure(call, e);
                if (callback != null) {
                    callback.onFailure(call, e);
                }
            }

            @Override
            public void onSuccess(PmListBody body) {
                if (callback != null) {
                    callback.onSuccess(body);
                }
            }

            @Override
            public void handleException(NgaException e) {
                super.handleException(e);
                if (callback != null) {
                    callback.handleException(e);
                }
            }
        });
    }

    /**
     * 请求阅读私信
     *
     * @param id       私信id
     * @param page     页数
     * @param callback 回调
     */
    public void read(long id, int page, Callback<PmReplyBody> callback) {
        NukeApi.pmRead(getClient(), new PmReadParam(id, page)).async(new JsonCallback<PmReplyBody>() {
            @Override
            public void onFailure(@NonNull Call call, @NonNull IOException e) {
                super.onFailure(call, e);
                if (callback != null) {
                    callback.onFailure(call, e);
                }
            }

            @Override
            public void onSuccess(PmReplyBody body) {
                TopicReadManager.saveCache(body.getData().getUserContext().getUserInfo());
                if (callback != null) {
                    callback.onSuccess(body);
                }
            }

            @Override
            public void handleException(NgaException e) {
                super.handleException(e);
                if (callback != null) {
                    callback.handleException(e);
                }
            }

        });
    }
}
